﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client_Server_Interface;

namespace BTM_Client_Application
{
    /// <summary>
    /// Логика взаимодействия для ClientEventItem.xaml
    /// </summary>
    public partial class ClientEventItem : IComparable
    {
        private ClientEventItem()
        {
            InitializeComponent();
        }
        private EventState _state;

        public event VoidBoolDelegate StateChangedEvent;

        protected virtual void OnStateChangedEvent(bool a)
        {
            VoidBoolDelegate handler = StateChangedEvent;
            if (handler != null) handler(a);
        }

        public ClientEventItem(string title, string info) : this()
        {
            Time = DateTime.Now;
            EventDate.Content = Time.ToShortTimeString();
            Title = title;
            EventTitle.Text = Title;
            Info = info;
            EventInfo.Text = Info;
            State = EventState.Unsolved;
        }

        public void Solved()
        {
            State = EventState.Solved;
            OnStateChangedEvent(true);
        }

        public EventState State
        {
            get { return _state; }
            set
            {
                switch (_state)
                {
                    case EventState.Solved:
                        TitleContainer.Background = new SolidColorBrush(Colors.LightCyan);
                        break;
                    case EventState.Unsolved:
                        TitleContainer.Background = new SolidColorBrush(Colors.Orange) {Opacity = 0.6};
                        break;
                }
                _state = value;
            }
        }

        public enum EventState
        {
            Solved,
            Unsolved
        }

        public bool CompareInfo(string title, string info)
        {
            return Info == info && Title == title;
        }

        public int CompareTo(object obj)
        {
            var another = obj as ClientEventItem;
            if (another == null)
                return 1;
            if (HashCode() == another.HashCode())
                return 0;
            return Time.CompareTo(another.Time);
        }

        public int HashCode()
        {
            return Title.GetHashCode() + Time.GetHashCode() + Info.GetHashCode();
        }

        public DateTime Time { get; private set; }

        public string Title { get; private set; }

        public string Info { get; private set; }
    }
}
